package in.muton.edushare.auth.roles;

import in.muton.edushare.EduShareSession;
import in.muton.edushare.url.URLLoader;
import in.muton.edushare.wicket.models.CayenneModel;

import java.io.Serializable;
import java.util.List;

import org.apache.wicket.RestartResponseException;
import org.apache.wicket.model.IModel;

import com.edushare.common.constants.URLConstants;
import com.edushare.common.dao.UserPollDAO;
import com.edushare.common.vo.CourseInstances;
import com.edushare.common.vo.Departments;
import com.edushare.common.vo.Groups;
import com.edushare.common.vo.Poll;
import com.edushare.common.vo.Universities;
import com.edushare.common.vo.Users;

public class PageRoleCheck implements Serializable{
	private static final long serialVersionUID = 1384246133809274498L;
	
	private static final int UNIV_CHECK = 1;
	private static final int DEPT_CHECK = 2;
	private static final int COURSE_CHECK = 3;
	private static final int POLL_CHECK = 4;
	private static final int GROUP_CHECK = 5;

	private int checkType = 0;
	private IModel<Users> userModel = null;
	private IModel<Universities> univModel = null;
	private IModel<Departments> deptModel = null;
	private IModel<CourseInstances> courseModel = null;
	private IModel<Poll> pollModel = null;
	private IModel<Groups> GroupModel = null;

	public PageRoleCheck(Universities univ, Users loggedInUser) {
		this.userModel = new CayenneModel<Users>(loggedInUser);
		univModel = new CayenneModel<Universities>(univ);
		checkType = UNIV_CHECK;
	}

	public PageRoleCheck(Departments dept, Users loggedInUser) {
		this.userModel = new CayenneModel<Users>(loggedInUser);
		deptModel = new CayenneModel<Departments>(dept);
		checkType = DEPT_CHECK;
	}

	public PageRoleCheck(CourseInstances inst, Users loggedInUser) {
		this.userModel = new CayenneModel<Users>(loggedInUser);
		courseModel = new CayenneModel<CourseInstances>(inst);
		checkType = COURSE_CHECK;
	}

	public PageRoleCheck(Poll poll, Users loggedInUser) {
		this.userModel = new CayenneModel<Users>(loggedInUser);
		pollModel = new CayenneModel<Poll>(poll);
		checkType = POLL_CHECK;
	}

	public PageRoleCheck(Groups groups, Users loggedInUser) {
		this.userModel = new CayenneModel<Users>(loggedInUser);
		GroupModel = new CayenneModel<Groups>(groups);
		checkType = GROUP_CHECK;
	}

	public void check(List<Integer> roles) {
		boolean OK = true;
		switch (checkType) {
		case UNIV_CHECK:
			OK = univAuth(roles);
			break;
		case DEPT_CHECK:
			OK = deptAuth(roles);
			break;
		case COURSE_CHECK:
			OK = courseAuth(roles);
			break;
		case POLL_CHECK:
			OK = pollAuth(roles);
			break;
		case GROUP_CHECK:
			OK = groupAuth(roles);
			break;
		}
		if (!OK)
			throw new RestartResponseException(URLLoader
					.getPageClass(URLConstants.UNAUTHORIZED_PATH));
		return;
	}

	private boolean univAuth(List<Integer> roles) {
		UnivRoleCheck urc = new UnivRoleCheck(univModel.getObject(), userModel
				.getObject());
		int role = urc.getRole();
		return contains(roles, role);
	}

	private boolean courseAuth(List<Integer> roles) {
		CourseRoleCheck crc = new CourseRoleCheck(courseModel.getObject(),
				userModel.getObject());
		if(crc.isAdmin())
			return true;
		int role = crc.getRole();
		return contains(roles, role);
	}

	private boolean deptAuth(List<Integer> roles) {
		DeptRoleCheck drc = new DeptRoleCheck(deptModel.getObject(), userModel
				.getObject());
		if(drc.isUnivAdmin())
			return true;
		int role = drc.getRole();
		return contains(roles, role);
	}

	private boolean pollAuth(List<Integer> roles) {
		UserPollDAO upDAO = new UserPollDAO(EduShareSession.get()
				.getSessionORMInterface());
		int role = upDAO.getUserRole(pollModel.getObject(), userModel
				.getObject());
		return contains(roles, role);
	}

	private boolean groupAuth(List<Integer> roles) {
		int role = userModel.getObject().getGroupRole(GroupModel.getObject());
		return contains(roles, role);
	}

	private boolean contains(List<Integer> roles, int role) {
		for (Integer i : roles) {
			if (i.intValue() == role)
				return true;
		}
		return false;
	}
}
